﻿using BookModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;

namespace DAL
{
    //图书类型编号
   
   public class TypeInfoDAL
   {
       #region 查询所有类型
       public List<TypeInfo> GetTYpeInfo()
       {
           string sql = "select*from TypeInfo";
           List<TypeInfo> lt = new List<TypeInfo>();
           if (SQLHelp.OpenConn())
           {
               SqlDataReader sdr = SQLHelp.ExecReader(sql);
               if (sdr!=null)
               {
                   while (sdr.Read())
                   {
                       lt.Add(new TypeInfo 
                       {
                           TID=(int)sdr[0],
                           TName=sdr[1] as string,
                           Tremark=sdr[2] as string
                       });
                   }
               }
               SQLHelp.CloseConn();
           }
           return lt;
       }
       #endregion

       #region 新增类型
       public string InsertTypeInfo(TypeInfo ti)
       {
           string sql = "insert into TypeInfo values('"+ti.TName+"','"+ti.Tremark+"')";
           int count = SQLHelp.ExecQuery(sql);
           switch (count)
           {
               case-2:
                   sql= "数据库打开失败，请联系管理员";
                   break;
               case -1:
                   sql = "数据输入出错，请检查语句";
                   break;
               case 0:
                   sql= "无任何添加";
                   break;
           }
           return sql;
       }
       #endregion
       #region 修改类型
       public string UpdateTypeInfo(TypeInfo ti)
       {
           string sql = "update TypeInfo set Tremark='"+ti.Tremark+"',Tname='"+ti.TName+"' where TID='"+ti.TID+"'";
           int count = SQLHelp.ExecQuery(sql);
           switch (count)
           {
               case -2:
                   sql= "数据库打开失败，请联系管理员";
                   break;
               case -1:
                   sql= "数据输入出错，请检查语句";
                   break;
               case 0:
                   sql= "无任何添加";
                   break;
               default:
                   sql = "修改成功"+count+"条数据";
                   break;
           }
           return sql;
       }
       #endregion
   }
}
